<?php

namespace App\Models\System;

use App\Models\Model;
use Illuminate\Support\Facades\DB;
use App\Models\System\RoleUser;

class Menu2 extends Model
{


    protected $table = 'menus'; 

    /**
     * 菜单专用
     * @author lxy
     * @datetime 2023-02-06
     */
    public function children()
    {
        static  $menuids = [];
        if(!$menuids){
            $user = auth()->user();
            $role_id = $user->curr_role_id;
            if(!$role_id){
                $role_id = RoleUser::where('user_id',$user->id)->first();
                if(!$role_id){
                    $role_id = 0;
                }
                $role_id = $role_id->role_id;
            }
           
            $menuids = DB::table('rolepowers')->where('role_id',$role_id)->get()->pluck('actionid')->toArray();
            if(!$menuids){
                $menuids = [];
            }      
        }
        
        return $this->hasMany(self::class,'pid','id')
                ->select('menus.id','menus.pid','menus.name as title','menus.icon','menus.path as href','menus.is_ml as type','menus.openType')
                ->whereStatus(1)
                ->whereIn('id',$menuids)
                ->whereType(1)
                ->orderBy('sort')
                ->with(['children']);
    }


}
